define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'circle/circle_list/index' + location.search,
                    add_url: 'circle/circle_list/add',
                    edit_url: 'circle/circle_list/edit',
                    del_url: 'circle/circle_list/del',
                    multi_url: 'circle/circle_list/multi',
                    import_url: 'circle/circle_list/import',
                    table: 'circle_type',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'updatetime',
                sortOrder:'desc',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'cir_id', title: __('圈子类型'), visible: true, addClass: "selectpage", extend: "data-source='circle/type/index' data-field='name'",  formatter: function(value, row, index){
                                var aa = Config.typearr;
                                return aa[value];
                            }, sortable: true},
                        {field: 'uid', title: __('创建用户'), visible: true, addClass: "selectpage", extend: "data-source='user/user/index' data-field='nickname'",  formatter: function(value, row, index){
                                var aa = Config.userarr;
                                return aa[value];
                            }, sortable: true},
                        {field: 'xiao_id', title: __('所属校园'), visible: true, addClass: "selectpage", extend: "data-source='main/school/index' data-field='name'",  formatter: function(value, row, index){
                                var aa = Config.schoolarr;
                                return aa[value];
                            }, sortable: true},
                        {field: 'content', title: __('圈子内容'), sortable: true,limit:10,windth:20,formatter:function (value,row,index) {
                                if (value.length <= 20) {
                                    return value;
                                }
                                var truncatedString = value.substring(0, 20) + '...';
                                return truncatedString;
                            }},
                        {field: 'pics', title: __('图片集'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'video', title: __('圈子视频'), sortable: false,limit:10,windth:20,formatter:function (value,row,index) {
                                if (value.length <= 10) {
                                    return value;
                                }
                                var truncatedString = value.substring(0, 10) + '...';
                                return truncatedString;
                            }},
                        {field: 'weixin', title: __('联系微信'), sortable: true},
                        {field: 'extend', title: __('其他信息'), operate: false,sortable: true,  formatter: function(value, row, index){
                                var aa= Config.typeextendarr;
                                var sts = '' ;
                                if(value){
                                    var decodedString = value.replace(/&amp;quot;/g, '"');
                                    var decodedString2 = decodedString.replace(/&quot;/g, '"');
                                    var jsonObject = JSON.parse(decodedString2);
                                    console.log(jsonObject);
                                    console.log(aa);
                                    for (var key in jsonObject) {
                                        var value = jsonObject[key];
                                        if(value){
                                            for (var v in value) {
                                                sts += v +" : " + value[v] +  "<br>\r\n";
                                            }
                                        }
                                    }
                                    return sts;
                                }else {
                                    return "";
                                }
                            }},
                        {field: 'view', title: __('浏览次数'), operate: false, sortable: true},
                        {field: 'is_hot', title: __('是否热门'), operate: false, formatter: Table.api.formatter.toggle, searchList: {1: __('热门'), 0: __('非热门')}, sortable: true},
                        {field: 'status', title: __('状态'), operate: false, formatter: Table.api.formatter.toggle, searchList: {1: __('正常'), 0: __('锁定')}, sortable: true},
                        {field: 'createtime', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'updatetime', title: __('更新时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                thumb: function (value, row, index) {
                    var html = '';
                    if (row.pics.indexOf("image") > -1) {
                        html = '<a href="' + row.pics + '" target="_blank"><img src="' + row.pics + row.pics + '" alt="" style="max-height:60px;max-width:120px"></a>';
                    } else {
                        html = '<a href="' + row.pics + '" target="_blank"><img src="' + Fast.api.fixurl("ajax/icon") + "?suffix=" + row.pics + '" alt="" style="max-height:90px;max-width:120px"></a>';
                    }
                    return '<div style="width:120px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + html + '</div>';
                },
                title: function (value, row, index) {
                    value = value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                    var caret = row.haschild == 1 || row.ismenu == 1 ? '<i class="fa fa-caret-right"></i>' : '';
                    value = value.indexOf("&nbsp;") > -1 ? value.replace(/(.*)&nbsp;/, "$1" + caret) : caret + value;

                    value = !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
                    return '<a href="javascript:;" data-id="' + row.id + '" data-pid="' + row.pid + '" class="'
                        + (row.haschild == 1 || row.ismenu == 1 ? 'text-primary' : 'disabled') + ' btn-node-sub">' + value + '1111</a>';
                }
            }
        }
    };
    return Controller;
});